import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

// 不同的角色登录网站看到的菜单都是一样的，因为注册的路由是死的
/*
  活的路由：不同的用户登录时会向服务器发请求获取其相应的权限信息，根据返回的权限信息动态的去设置路由
 */
// 常量路由：所有的用户都可以看到的路由
export const constantRoutes = [{
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: {
        title: '首页',
        icon: 'dashboard'
      }
    }]
  },
]
// 异步路由：根据不同的用户需要筛选过滤出的路由
export const asyncRoutes = [{
    path: '/product',
    component: Layout,
    name: 'Product',
    meta: {
      title: '商品管理',
      icon: 'el-icon-goods'
    },
    children: [{
        path: 'trademark',
        name: 'TradeMark',
        component: () => import('@/views/product/tradeMark'),
        meta: {
          title: '品牌管理'
        }
      },
      {
        path: 'attr',
        name: 'Attr',
        component: () => import('@/views/product/Attr'),
        meta: {
          title: '平台属性管理'
        }
      },
      {
        path: 'spu',
        name: 'Spu',
        component: () => import('@/views/product/Spu'),
        meta: {
          title: 'spu管理'
        }
      },
      {
        path: 'sku',
        name: 'Sku',
        component: () => import('@/views/product/Sku'),
        meta: {
          title: 'sku管理'
        }
      }
    ]
  },
  {
    path: '/acl',
    component: Layout,
    name: 'Acl',
    meta: {
      title: '权限管理',
      icon: 'el-icon-setting'
    },
    children: [{
      path: 'user',
      name: 'user',
      component: () => import('@/views/acl/user'),
      meta: {
        title: '用户管理'
      }
    }, {
      path: 'role',
      name: 'role',
      component: () => import('@/views/acl/role'),
      meta: {
        title: '角色管理'
      }
    }, {
      path: 'permission',
      name: 'permission',
      component: () => import('@/views/acl/permission'),
      meta: {
        title: '菜单管理'
      }
    }]
  },
]
//任意路由：路径出现错误的时候重定向到404
export const anyRoutes = {
  path: '*',
  redirect: '/404',
  hidden: true
}

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({
    y: 0
  }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
